﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BullTasDesaDal.Entities;
using NHibernate.Criterion;
using NHibernate.SqlCommand;
using System.Data;

namespace BullTasDesaDal.Service
{
    public class AnalysisService
    {
        public static IList<Analysis> Get(Int32 idAppraisal)
        {

            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                return session.CreateCriteria(typeof(Analysis))
                    .Add(Restrictions.Eq("Appraisal.Id", idAppraisal))
                    .List<Analysis>();

            }

        }

        public static Analysis GetById(Int64 idAnalysis)
        {

            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                return session.CreateCriteria(typeof(Analysis))
                    .Add(Restrictions.Eq("Id", idAnalysis))
                    .UniqueResult<Analysis>();

            }

        }

        public static Analysis GetLastAnalysis(Int32 idAppraisal)
        {
            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                return session.CreateCriteria(typeof(Analysis))
                    //.CreateAlias("Lands","land")
                    //.CreateAlias("Betterments", "betterment")
                    //.SetFetchMode("Lands", NHibernate.FetchMode.Default)
                    //.SetFetchMode("Betterments", NHibernate.FetchMode.Default)
                    .Add(Restrictions.Eq("Appraisal.Id", idAppraisal))
                    .AddOrder(Order.Desc("Version"))
                    //.List<Analysis>()
                    //.FirstOrDefault();
                    //.Max(x => x.Version);
                    .SetMaxResults(1)
                    .UniqueResult<Analysis>();
            }
        }

        public static IList<LandCategory> GetAllLandCategorys()
        {

            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                return session.CreateCriteria(typeof(LandCategory))
                    .List<LandCategory>();

            }

        }

        public static IList<Land> GetLandAnalysis(Int64 idAppraisal, Int16 version)
        {
            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                return session.CreateCriteria(typeof(Land))
                    .Add(Restrictions.Eq("IdAppraisal", idAppraisal))
                    .List<Land>();


            }
        }

        public static void Save(Analysis analysis)
        {
            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                using (NHibernate.ITransaction transaction = session.BeginTransaction())
                {
                    //session.CreateCriteria(typeof(Analysis))
                    //    .SetFetchMode("Lands", NHibernate.FetchMode.Lazy)
                    //    .SetFetchMode("Betterments", NHibernate.FetchMode.Lazy);
                       
                    session.Save(analysis);
                    transaction.Commit();
                }
            }
        }

       
    }
}
